package com.easybuilder.blog.service.impl;

import com.easybuilder.blog.dao.BlogCommentDao;
import com.easybuilder.blog.dto.BlogCommentDTO;
import com.easybuilder.blog.dto.BlogReplyDTO;
import com.easybuilder.blog.entity.BlogComment;
import com.easybuilder.blog.entity.BlogReply;
import com.easybuilder.blog.service.CommentService;
import com.easybuilder.blog.service.ReplyService;
import com.easybuilder.common.utils.TimeUtil;
import com.easybuilder.common.utils.UserUtil;
import com.easybuilder.core.rbac.entity.SysUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private BlogCommentDao commentDao;
    @Autowired
    private ReplyService replyService;

    @Override
    public BlogComment articleComment(BlogComment comment) {
        // 设置评论时间
        comment.setCreateTime(new Date());
        SysUser user = UserUtil.getUser();
        if (user != null) {
            comment.setUserid(user.getId());
            comment.setNickname(user.getNickname());
            comment.setUserImg(user.getImg());
        }
        commentDao.insertSelective(comment);
//        ArticleCommentDTO commentDTO = new ArticleCommentDTO();
//        BeanUtils.copyProperties(comment, commentDTO);
//        if (user != null) {
//            // 设置评论用户头像
//            commentDTO.setImg(user.getImg());
//        }
        return comment;
    }

    /** 获取评论列表
     * @return*/
    @Override
    public List<BlogCommentDTO> getCommentByArticle(Integer worksType, Long articleId) {
        Example example = new Example(BlogComment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("worksId", articleId);
        criteria.andEqualTo("worksType", worksType);
        criteria.andEqualTo("dropStatus", 0);
        example.orderBy("createTime").desc();
        List<BlogComment> comments = commentDao.selectByExample(example);
        List<BlogCommentDTO> result = new ArrayList<>();
        if (comments != null && comments.size() > 0) {
            for (BlogComment comment : comments) {
                BlogCommentDTO commentDTO = new BlogCommentDTO();
                BeanUtils.copyProperties(comment, commentDTO);
                // 设置评论回复
                setCommentReply(commentDTO, comment.getId());
                result.add(commentDTO);
            }
        }
        return result;
    }

    @Override
    public List<Integer> countComment() {

        return commentDao.countComment(TimeUtil.getFormatDay(new Date()));
    }

    /** 设置评论回复 */
    private void setCommentReply(BlogCommentDTO commentDTO, Long id) {
        List<BlogReplyDTO> blogReplys = replyService.selectByTypeAndReplyId(0, id);
        if (blogReplys != null && blogReplys.size() > 0) {
            commentDTO.setReplyCount(blogReplys.size());
            commentDTO.setReplyList(blogReplys);
            // 设置评论的回复为第一层级
            for (BlogReplyDTO blogReply : blogReplys) {
                blogReply.setFirstLayer(true);
            }
        } else {
            commentDTO.setReplyCount(0);
        }
    }

}
